<?php
/**
 * @author jim
 * @date 2017/11/25
 */

namespace app\admin\controller;
use app\admin\model\Company;
use app\admin\model\CompanyIncome;

class Income extends Common
{
    public function index() {
        if($_POST || !$_GET['page']){
            session('searchParam',null);
        }
        $sessionParam = session('searchParam');
        $sessionArr['status'] = @$_POST['status'] ? $_POST['status'] : @$sessionParam['status'];
        $sessionArr['start']  = @$_POST['start'] ? $_POST['start'] : @$sessionParam['start'];
        $sessionArr['end']  = @$_POST['end'] ? $_POST['end'] : @$sessionParam['end'];
        session('searchParam',$sessionArr);
        $page = @$_GET['page'] ? $_GET['page'] : 1;
        $where = [];

        if($sessionArr['status'] == "商城收益")
        {
            $where['source'] = 2;
        }
        else if($sessionArr['status'] == "藏宝收益")
        {
            $where['source'] = 1;
        }
        else {
            $where['source'] = ['in','1,2'];
        }
        if($sessionArr['start'] && $sessionArr['end']){
            $where['addtime'] = ['between',[$sessionArr['start'],$sessionArr['end'].' 23:59:59']];
            $wheredata['addtime'] =  $where['addtime'];
        }

        $companyMoney_Model = new CompanyIncome();
        $wheredata['cpid'] =  session('company_id');
        if($where['source'] == '1')
        {
            //藏宝收入
            $wheredata['source'] = 1;
            $company_treasureSum = $companyMoney_Model->sumData($wheredata,'money');
            $company_moneySum = $company_treasureSum;
        }
        else if($where['source'] =='2')
        {
            //商城收入
            $wheredata['source'] = 2;
            $company_shopSum = $companyMoney_Model->sumData($wheredata,'money');
            $company_moneySum = $company_shopSum;
        }
        else
        {
            //藏宝收入
            $wheredata['source'] = 1;
            $company_treasureSum = $companyMoney_Model->sumData($wheredata,'money');
            //商城收入
            $wheredata['source'] = 2;
            $company_shopSum = $companyMoney_Model->sumData($wheredata,'money');
            $company_moneySum = $company_treasureSum + $company_shopSum;
        }

        $where['cpid'] = session('company_id');
        $companyIncomeModel = new CompanyIncome();
        $resList = $companyIncomeModel->searchData($where,$page,$this->pernum,'Id asc');
        $totalNum = $companyIncomeModel->getTotalNum($where);
        $pageShow = $companyIncomeModel->paginate($this->pernum,$totalNum)->render();

        $this->assign('company_moneySum',$company_moneySum);
        $this->assign('company_shopSum',$company_shopSum);
        $this->assign('company_treasureSum',$company_treasureSum);

        $this->assign('pageShow',$pageShow);
        $this->assign('page',$page);
        $this->assign('resList',$resList);
        $this->assign('postArr',$sessionArr);
        $tmpdata =  array('1'=>'藏宝收益','2'=>'商城收益');
        $this->assign('inComeRes',$tmpdata);
        return $this->fetch();

    }

    // 公司收益详情
    public function companyIncomeDetail() {
        // 获取公司Id
        $cpid = input('cpid');


        $_POST['cpid'] = $cpid;

        // 获取公司的基本数据
        $companyModel = new Company();
        $companyInfo = $companyModel->findData(array('id'=>$cpid));

        $this->assign('CompanyTypeId',$companyInfo['CompanyTypeId']);

        $this->assign('CompanyName',$companyInfo['CompanyName']);

        // 获取收益详情，1藏宝、2商城、3推荐股东、4直播、5游戏
        // 按照时间降序
        $where = array();
        $whereStart = array();
        $whereEnd = array();
        $where['cpid'] = $cpid;

        // 定义存储查询条件的数组
        $conditionArr = array();
        $conditionArr['cpid'] = $cpid;


        // 列表查询条件
        if (input('sSource') != 0) {
            $conditionArr['sSource'] = input('sSource');
            $where['source'] = input('sSource');
            $this->assign('sSource',input('sSource'));
        }

        $this->assign('start',input('start'));
        $this->assign('end',input('end'));
        if (input('start')) {
            $conditionArr['start'] = input('start');
            $whereStart['addtime'] = ['>',input('start').' 00:00:00'];

        }

        if (input('end')) {
            $conditionArr['end'] = input('end');
            $whereEnd['addtime'] = ['<',input('end').' 23:59:59'];
        }



        $pagesize = config('default_page_size');
        $res = $this->db_app->table('company_income')
            ->where($where)
            ->where($whereStart)
            ->where($whereEnd)
            ->order('addtime desc')
            ->field('money,source,CASE source
                WHEN 1 THEN "藏宝"
                WHEN 2 THEN "商城"
                WHEN 3 THEN "推荐股东"
                WHEN 4 THEN "直播"
                WHEN 5 THEN "游戏"
                ELSE "其他"
                END as sourceName
            ,addtime')
            ->paginate($pagesize,false,[
                'query' => $conditionArr
            ]); // 防止参数丢失

        $page = $res->render(); // 分页
        $resList = $res->all(); // 数据

//        dump($this->db_app->getLastSql());



        $this->assign('page',$page);
        $this->assign('resList',$resList);


        $this->view->engine->layout(false);
        return $this->fetch();
    }
}